
package com.mengmeizi;

import com.mengmeizi.util.Const;
import com.mengmeizi.util.MailTool;
import com.mengmeizi.util.Tools;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import javax.mail.MessagingException;

public class ActFeedback extends Activity implements OnClickListener, Callback {

    private SoftApplication application;

    private ImageView imvTitle;

    private EditText etContent;

    private Button btnSubmit;

    private Handler handler;

    private boolean isShowLoading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_submitfeedback);

        application = (SoftApplication) getApplication();

        application.addActivity(this);

        handler = new Handler(this);

        initView();

    }

    private void initView() {
        imvTitle = (ImageView) findViewById(R.id.imvTitle);
        imvTitle.setOnClickListener(this);

        etContent = (EditText) findViewById(R.id.etContent);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof Button) {
            if (v == btnSubmit) {
                Tools.hideKeyboard(this);
                final String content = etContent.getText().toString();
                if (content == null || content.equals("")) {
                    Tools.showShortToast(this, getString(R.string.inputEmpty));
                } else {
                    handler.sendEmptyMessage(Const.DIALOG_SHOW);
                    new Thread() {

                        @Override
                        public void run() {
                            // 提交
                            try {
                                MailTool.sendMail(handler, getString(R.string.zhengdianFeedbackInfo), content);
                            } catch (MessagingException e) {
                                e.printStackTrace();
                            }
                            super.run();
                        }

                    }.start();

                }
            }
        } else if (v instanceof ImageView) {
            if (v == imvTitle) {
                Intent intent = new Intent(this, ActMore.class);
                startActivity(intent);
                this.finish();
            }
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case Const.SEND_MAIL_SUCCESS:
                handler.sendEmptyMessage(Const.DIALOG_DISMISS);
                Tools.showShortToast(this, getString(R.string.submitSuccess));
                ActFeedback.this.finish();
                break;
            case Const.DIALOG_SHOW:
                isShowLoading = true;
                showDialog(1000);
                break;
            case Const.DIALOG_DISMISS:
                if (isShowLoading) {
                    dismissDialog(1000);
                    isShowLoading = false;
                }
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        isShowLoading = false;
        super.onDestroy();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // if (id == Const.LOADING_DIALOG_ID) {
        ProgressDialog loadingDialog = new ProgressDialog(this);
        // loadingDialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.refresh_round));
        loadingDialog.setMessage(getString(R.string.loading));
        loadingDialog.setIndeterminate(false);// 设置进度条是否为不明确（没有明确的数字来表示进度）
        return loadingDialog;
        // }
        // return super.onCreateDialog(id);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, ActMore.class);
            startActivity(intent);
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
